package com.example.hospitalmanagement.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.bean.DiagnosisAndTreatmentItem

/**
 * 预约挂号内部条目所用适配器，此适配器在门诊综合、门诊查询、价格公示中被复用
 */
class RegisterActivityAdapter(context: Context) :
    RecyclerView.Adapter<RegisterActivityAdapter.RegisterHolder>() {
    private val context = context
    var dataList: MutableList<DiagnosisAndTreatmentItem> = mutableListOf()
    private lateinit var listener: (position: Int) -> Unit
    fun setListener(listener: (position: Int) -> Unit) {
        this.listener = listener
    }

    class RegisterHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val registerIcon: ImageView = itemView.findViewById(R.id.register_icon)
        val registerName: TextView = itemView.findViewById(R.id.register_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RegisterHolder {
        val registerHolder = RegisterHolder(
            LayoutInflater.from(context).inflate(R.layout.register_item, parent, false)
        )
        registerHolder.itemView.setOnClickListener {
            listener(registerHolder.adapterPosition)
        }
        return registerHolder
    }

    override fun onBindViewHolder(holder: RegisterHolder, position: Int) {
        holder.registerIcon.setImageResource(dataList[position].itemIcon)
        holder.registerName.text = dataList[position].itemName
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}